package com.dingreading.cloud.mall.service.impl;

import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.QueryFiledUtil;
import com.dingreading.cloud.mall.entity.BalanceLog;
import com.dingreading.cloud.mall.entity.table.BalanceLogTableDef;
import com.dingreading.cloud.mall.mapper.BalanceLogMapper;
import com.dingreading.cloud.mall.service.BalanceLogService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class BalanceLogServiceImpl extends BaseServiceImpl<BalanceLogMapper, BalanceLog, BalanceLogTableDef> implements BalanceLogService {

    @Override
    protected BalanceLogTableDef getTable() {
        return BalanceLogTableDef.balanceLog;
    }

    @Override
    public BalanceLog getByCode(String code) {
        return getMapper().selectOneByCondition(table.code.eq(code));
    }

    @Override
    public Page<BalanceLog> pageList(PageUtil pageUtil, Integer projectType, Integer income, String keyword, Integer payType, Integer status, String beginTime, String endTime) {

        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.code.like(keyword)
                    .or(table.orderCode.like(keyword))
                    .or(table.phone.like(keyword)));
        if (null != projectType)
            condition.and(table.projectType.eq(projectType));
        if (null != income)
            condition.and(table.income.eq(income));
        if (null != payType)
            condition.and(table.payType.eq(payType));
        if (null != status)
            condition.and(table.status.eq(status));
        if (StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime))
            condition.and(table.payTime.between(QueryFiledUtil.appendMin00(beginTime), QueryFiledUtil.appendMin24(endTime)));

        QueryWrapper queryWrapper = QueryWrapper.create()
                .select()
                .from(table)
                .orderBy(table.id.desc())
                .where(condition);

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), queryWrapper);
    }

}
